import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait,Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from icecream import ic
from selenium.common.exceptions import NoSuchElementException

def answer(driver):
       for nr in range(10):
        tips = driver.find_element_by_css_selector('[class="tips"]')
        tips.click()
        time.sleep(2)
        tips_txt = driver.find_element_by_css_selector('[class="line-feed"]').text
        tips_red = driver.find_elements_by_css_selector('[class="line-feed"]>font')
        time.sleep(2)
        tips.click()
        t_list = []
        # ic(nr)
        for t in tips_red:
            t_list.append(t.text)
        t_s = ''.join(t_list)
        ic(t_s,t_list)
        sel_ans = driver.find_elements_by_css_selector('[class*="q-answer choosable"]')
        ic(sel_ans)
        if sel_ans == []:
            ic('select is empty')
            #视频题        
            if t_list[0] == '请观感视频':
                driver.refresh()
                nr=0
                driver.find_element_by_css_selector('body').send_keys(Keys.ENTER)
            #填空题
            else:
                inputs = driver.find_elements_by_css_selector('[class="blank"]')
                ic(inputs)
                for in_ans in range(len(inputs)):
                    ic(in_ans,t_list[in_ans])
                    inputs[in_ans].click()
                    time.sleep(1)
                    inputs[in_ans].send_keys(t_list[in_ans])
                    # ActionChains(driver).move_to_element(inputs[in_ans]).send_keys(t_list[in_ans]).perform()
        
        else:
            #选择题
            import re
            for a in sel_ans:
                pattern = re.match( r'^[A-Z]\.\s(.*)',a.text, re.M|re.I)
                a_t = pattern.group(1)
                ic(a_t)
                if re.search(r'.*(%s)'%a_t,t_s):
                    a.click()
                    # time.sleep(2)
                    # ic('yes')
                time.sleep(2)
            

        driver.find_element_by_css_selector('[class="ant-btn next-btn ant-btn-primary"]').click()
        time.sleep(5)

chrome_options = webdriver.ChromeOptions()
# 使用headless无界面浏览器模式
# chrome_options.add_argument('--headless') #增加无界面选项
chrome_options.add_argument('--disable-gpu') #如果不加这个选项，有时定位会出现问题

# 启动浏览器，获取网页源代码
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.implicitly_wait(10)
driver.maximize_window()
#进入积分页
mainUrl = "https://pc.xuexi.cn/points/my-points.html"
driver.get(mainUrl)

#我要选读文章
driver.find_element_by_css_selector('body').send_keys(Keys.ENTER)
# time.sleep(10)
title =driver.find_elements_by_css_selector('[class="my-points-card-title"]')
txt = driver.find_elements_by_css_selector('[class="big"]')
# for i in range(1,7):
if title[1].text == "我要选读文章" and txt[1].text == "去看看":
    print('我要选读文章')
    driver.get('https://www.xuexi.cn')
    time.sleep(5)
    # driver.find_element_by_link_text("环球视野").click()
    # time.sleep(5)
    # toHandle = driver.window_handles
    # print(toHandle)
    # driver.switch_to_window(toHandle[1])
    read = driver.find_elements_by_css_selector('[class="text-link-item-title"]')
    for j in range(17,24):
        print(read[j].text)
        read[j].click()
        time.sleep(5)
        toHandle = driver.window_handles
        driver.switch_to_window(toHandle[1])
        driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")
        time.sleep(70)
        driver.close()
        driver.switch_to_window(toHandle[0])

#视听学习
driver.get(mainUrl)
driver.find_element_by_css_selector('body').send_keys(Keys.ENTER)
time.sleep(10)
title =driver.find_elements_by_css_selector('[class="my-points-card-title"]')
txt = driver.find_elements_by_css_selector('[class="big"]')
if title[2].text == "视听学习" and txt[2].text == "去看看":
    driver.get('https://www.xuexi.cn')
    time.sleep(5)
    driver.find_element_by_link_text("学习电视台").click()
    time.sleep(5)
    toHandle = driver.window_handles
    print(toHandle)
    driver.switch_to_window(toHandle[1])
    # driver.current_url
    # url = driver.current_url
    # print(url)
    driver.find_element_by_css_selector('[class="item-text"]').click()
    time.sleep(60)
    toHandle = driver.window_handles
    print(toHandle)
    driver.switch_to_window(toHandle[2])
    video = driver.find_elements_by_css_selector('[class="innerPic"]')
    for v in range(1,7):
        video[v].click()
        time.sleep(2)
        toHandle = driver.window_handles
        driver.switch_to_window(toHandle[3])
        driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")
        time.sleep(70)
        driver.close()
        driver.switch_to_window(toHandle[2])
    driver.close()
    driver.switch_to_window(toHandle[1])
    driver.close()
    driver.switch_to_window(toHandle[0])

# 每日答题
driver.get(mainUrl)
title =driver.find_elements_by_css_selector('[class="my-points-card-title"]')
txt = driver.find_elements_by_css_selector('[class="big"]')
if title[4].text == "每日答题" and txt[4].text == "去答题": 
    driver.get('https://pc.xuexi.cn/points/exam-practice.html')
for nr in range(5):
    tips = driver.find_element_by_css_selector('[class="tips"]')
    tips.click()
    time.sleep(2)
    tips_txt = driver.find_element_by_css_selector('[class="line-feed"]').text
    tips_red = driver.find_elements_by_css_selector('[class="line-feed"]>font')
    time.sleep(2)
    tips.click()
    t_list = []
    # ic(nr)
    for t in tips_red:
        t_list.append(t.text)
    t_s = ''.join(t_list)
    ic(t_s,t_list)
    sel_ans = driver.find_elements_by_css_selector('[class*="q-answer choosable"]')
    ic(sel_ans)
    if sel_ans == []:
        ic('select is empty')
        #视频题        
        if t_list[0] == '请观看视频':
            driver.refresh()
            nr=0
            driver.find_element_by_css_selector('body').send_keys(Keys.ENTER)
        #填空题
        else:
            inputs = driver.find_elements_by_css_selector('[class="blank"]')
            ic(inputs)
            for in_ans in range(len(inputs)):
                ic(in_ans,t_list[in_ans])
                inputs[in_ans].click()
                time.sleep(1)
                inputs[in_ans].send_keys(t_list[in_ans])
                # ActionChains(driver).move_to_element(inputs[in_ans]).send_keys(t_list[in_ans]).perform()
    
    else:
        #选择题
        import re
        for a in sel_ans:
            pattern = re.match( r'^[A-Z]\.\s(.*)',a.text, re.M|re.I)
            a_t = pattern.group(1)
            ic(a_t)
            if re.search(r'.*(%s)'%a_t,t_s):
                a.click()
                # time.sleep(2)
                # ic('yes')
            time.sleep(2)
        

    driver.find_element_by_css_selector('[class="ant-btn next-btn ant-btn-primary"]').click()
    time.sleep(5)

# 专项答题

driver.get(mainUrl)
title =driver.find_elements_by_css_selector('[class="my-points-card-title"]')
txt = driver.find_elements_by_css_selector('[class="big"]')
if title[6].text == "专项答题" and txt[6].text == "去答题": 
    driver.get('https://pc.xuexi.cn/points/exam-paper-list.html')
    try:
        red_bt = driver.find_element_by_css_selector('[class="ant-btn button ant-btn-primary"]')
        red_bt.click()
        time.sleep(5)
        # driver.current_url
        url = driver.current_url
        driver.get(url)
        answer(driver)
        driver.get(mainUrl)
    except NoSuchElementException as error:
        print('no new qus')

    contiune_bt = driver.find_element_by_css_selector('[class="ant-btn button ant-btn-primary ant-btn-background-ghost"]')
    if contiune_bt.text == '继续答题':
        contiune_bt.click()
        time.sleep(5)
        url = driver.current_url
        driver.get(url)
        answer(driver)
        driver.get(mainUrl)